package com.offer;

import com.bean.TreeNode;

/**
 * @Author: huangzhigao
 * @Date: 2023/3/4 20:22
 */
public class offer68 {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        TreeNode ancestor = root;
        while (ancestor != null) {
            if (p.val > ancestor.val && q.val > ancestor.val) {
                ancestor = ancestor.right;
            } else if (p.val < ancestor.val && q.val < ancestor.val) {
                ancestor = ancestor.left;
            } else {
                break;
            }
        }
        return ancestor;
    }
}
